//ipPort = '10.58.89.1:9090';
//ipPort = '10.58.88.212:8080';
Ext.define('Status.Screen', {
	extend: 'Ext.panel.Panel',
	alias: 'status.screen',
	
	initComponent: function(){
		me_status = this;
		Ext.apply(me_status, {
//			layout: 'vbox',
			height: '100%',
			width: '100%',
			autoScroll: true,
			dockedItems: [{
                xtype: 'toolbar',
                dock: 'top',
                items: [{
                    text: 'Clear completed',
                    handler: function() { // delete subcases from statuspanel, that are completed
                        var statpan = me_status.getComponent('statusPanel');
                        var numberOfItems = statpan.items.length;
                        for (m = (numberOfItems - 1); m > -1; m--){
                        	if (statpan.items.items[m].items.items[1].value !== "<img src='icons/load.gif'>") { // if not loading, it's completed
                        		if (statpan.items.items[m].items.items[0].value != 'Subcase')
                        			me_status.deleteRow(statpan.items.items[m].id); 
                        	};
                        };
                   }
                }]
			}],
            items: [{
                    xtype: 'panel',
                    id: 'statusPanel',
        			border: 0,
                    autoScroll: true,
                    height: '100%',
                    width: '100%',
                    name: 'statusPanel'
            }]
		});
		
		this.callParent(arguments);
	},
	
	addRow: function(caseId, subCaseId, caseName, subCaseName){ // add a row for a new running subcase
		var statpan = this.getComponent('statusPanel');
		// if it is already existing, remove it and start new
		var row = statpan.getComponent(subCaseId);
		if (row && row.id == subCaseId) this.deleteRow(subCaseId);
		var row = Ext.create(Ext.Panel,{
			id: subCaseId,
			caseId: caseId,
//			border: 0,
			layout: {
                    		type: 'hbox',
                    		pack: 'start'
                    	},
            items: [{
            	xtype: 'displayfield',
            	value: caseName + '/' + subCaseName,
            	flex: 8
            },{
            	xtype: 'displayfield',
            	id: 'status' + subCaseId,
            	value: "<img src='icons/load.gif'>",
            	height: '16px',
            	flex: 1
            },{
            	xtype: 'button',
            	id: 'details' + subCaseId ,
            	text: 'Details',
            	disabled: true,
            	flex: 2,
            	handler: function(){
            		// resultscreen needs both testcase and subcase data
            		me_status.setLoading('Loading', true);
    				var url = 'http://' + ipPort + '/bpclastresults/0/' + subCaseId;
    				caseData = null;
    				Ext.Ajax.request({ 
    					url: url,
    					method: 'GET',
    					success: function(response){
    						var obj = Ext.decode(response.responseText);
    						var url = 'http://' + ipPort + '/bpccase/' + caseId;
    						Ext.Ajax.request({ 
    							url: url,
    							method: 'GET',
    							success: function(response){
    								var caseData = Ext.decode(response.responseText);
    								var resultUI = Ext.create('case.result',{obj: obj, caseData: caseData, subCaseName: subCaseName});
    	    						var	resultWin = Ext.create('Ext.window.Window', {
    	    							title: 'Result',
    	    							closable: true,
    	    							animateTarget: me_status,
    	    							width: 1200,
    	    							height: 800,
    	    							borderStyle: 'padding:5px;',
    	    							shadown: false,
    	    							modal: true,
    	    							items: [
    	    								resultUI
    	    							]
    	    						});
    	    						resultWin.show(me_status, function(){
        								me_status.setLoading(false);
    	    						});
    							},
    							failure: function() {
    							},
    							callback: function() {
    								me_status.setLoading(false);
    							}
    						});    						
    					},
    					failure: function() {
    					},
    					callback: function() {
    						
    					}
    				})
            	}
            },{
            	xtype: 'button',
            	id: 'rerun' + subCaseId,
            	text: "<img src='http://www.icosky.com/icon/png/Media/Buttons/Button%20Reload.png' height='12' width='12'>",
            	textAlign: 'left',
            	disabled: true,
            	flex: 1,
            	handler: function(){
//            		statusScreen.addRow(caseId, subCaseId, caseName, subCaseNames[subCaseId]);
            		caseDetail.runSubCase(caseId, subCaseId, caseName, subCaseName);
            	}
            },{
            	xtype: 'button',
            	id: 'close' + subCaseId,
            	text: 'x',
            	textAlign: 'center',
            	disabled: true,
            	flex: 1,
            	handler: function(){
            		me_status.deleteRow(subCaseId);
            	}
            }]
		})
		statpan.add(row);
	},
	
	deleteRow: function(rowId){
		var statpan = this.getComponent('statusPanel');
		statpan.remove(rowId);
	},
		
	updateRow: function(subCaseId, status){
		var statpan = this.getComponent('statusPanel');
		var row = statpan.getComponent(subCaseId);
		var statusfield = row.getComponent('status' + subCaseId);
		var details = row.getComponent('details' + subCaseId);
		var close = row.getComponent('close' + subCaseId);
		var rerun = row.getComponent('rerun' + subCaseId);
		var icon;
		if (status == 'ok'){ 
			icon = "<img src='icons/ok.gif'>";
		} else if (status == 'fail') {
			icon = "<img src='icons/error.gif'>";
		} else if ((status == 'error') || (status == "error_profile")){
			icon = "<img src='icons/fail.gif'>";
		};
		statusfield.setValue(icon);
		// buttons are only enabled, if subcase is not running;
		if (status != 'error_profile' && status != 'error') details.enable();
		rerun.enable();
		close.enable();
	}
});
	
